using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OurMediaPlayer.Logic;
using DirectShowLib;
using OurMediaPlayer.View;
using OurMediaPlayer.Model;

namespace OurMediaPlayer
{
    public partial class TracksForm : SliderForm
    {
        public MainForm mainForm;

        public List<OurImage> images;

        public TracksForm()
        {
            InitializeComponent();
        }

        public TracksForm(Form owner, float fStep)
            : base(owner, fStep)
        {
            this.mainForm = (MainForm)owner;
            this.SlideDirection = ESlideDirection.Bottom;
            InitializeComponent();
        }

        public void ReloadInfo()
        {
            this.panel2.Controls.Clear();

            int i = 0;

            PictureBox box;

            if (this.mainForm.mediaManager.currentMedia != null)
            {
                List<OurImage> list = this.mainForm.mediaManager.currentMedia.segmentationImages;
                this.images = list;

                if (this.mainForm.mediaManager.currentMedia.fileType == Media.FileType.Video)
                {
                    foreach (OurImage img in list)
                    {
                        box = new PictureBox();
                        box.BackgroundImage = img.image;
                        box.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
                        box.Location = new System.Drawing.Point(i * 50, 62);
                        box.Margin = new System.Windows.Forms.Padding(0);
                        box.Name = "" + i;
                        box.Size = new System.Drawing.Size(50, 50);
                        box.DoubleClick += new System.EventHandler(this.pictureClick);
                        this.tracksToolTip.SetToolTip(box,Utils.SecondsToHHMMSS(img.time));

                        this.panel2.Controls.Add(box);

                        i++;
                    }
                }
                if (this.mainForm.mediaManager.currentMedia.fileType == Media.FileType.Video ||
                    this.mainForm.mediaManager.currentMedia.fileType == Media.FileType.Audio)
                {
                    box = new PictureBox();
                    box.BackColor = Color.Black;
                    box.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
                    box.Location = new System.Drawing.Point(0, 6);
                    box.Margin = new System.Windows.Forms.Padding(0);
                    box.Name = "Audio";
                    box.Size = new System.Drawing.Size(Math.Max(list.Count * 50, 50), 50);
                    // box.DoubleClick += new System.EventHandler(this.tracksForm.audioClick);
                    this.tracksToolTip.SetToolTip(box,Utils.SecondsToHHMMSS(this.mainForm.mediaManager.currentMedia.getDuration()));

                    this.panel2.Controls.Add(box);
                }
            }
        }

        public void pictureClick(object sender, EventArgs e)
        {
            int index = Int16.Parse(((PictureBox)sender).Name);
            this.mainForm.mediaManager.currentMedia.setPosition((this.images[index]).time);
        }

        private void AudioRadio_CheckedChanged(object sender, EventArgs e)
        {
            this.AudioRadio.Checked = true;
            this.VideoRadio.Checked = false;
            this.BothRadio.Checked = false;

            if (this.mainForm.mediaManager.currentMedia != null)
            {
                this.mainForm.mediaManager.currentMedia.videoWindow.put_Visible(OABool.False);
                this.mainForm.unmute();
            }
        }

        private void VideoRadio_CheckedChanged(object sender, EventArgs e)
        {
            this.AudioRadio.Checked = false;
            this.VideoRadio.Checked = true;
            this.BothRadio.Checked = false;

            if (this.mainForm.mediaManager.currentMedia != null)
            {
                this.mainForm.mediaManager.currentMedia.videoWindow.put_Visible(OABool.True);
                this.mainForm.mute();
            }
        }

        private void BothRadio_CheckedChanged(object sender, EventArgs e)
        {
            this.AudioRadio.Checked = false;
            this.VideoRadio.Checked = false;
            this.BothRadio.Checked = true;
            
            if (this.mainForm.mediaManager.currentMedia != null)
            {
                this.mainForm.mediaManager.currentMedia.videoWindow.put_Visible(OABool.True);
                this.mainForm.currentAudioState = MainForm.AudioState.Enabled;
            }
        }
    }
}